//
//  XMLUtils.java
//
//  Created by Noel Lynch on Thu Apr 08 2004.
package com.sceneworker.util.xml;

import  javax.swing.*;
import  java.awt.*;
import  java.io.*;

public class XMLUtils 
{
	static	private		String		mstr_tab = "";
	
	static  public  void	saveXMLTag(String str_tag, String str_data, FileWriter cl_f) {
		String  lstr_out = "<" + str_tag + ">" + str_data + "</" + str_tag + ">" + "\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
	}
	
	static  public  void	saveEmptyXMLTag(String str_tag, FileWriter cl_f) {
		String  lstr_out = "<" + str_tag + " />" + "\n";
		
		try	{
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
	}
	
	static  public  void	saveStartXMLTag(String str_tag, FileWriter cl_f) {
		String  lstr_out = "<" + str_tag + ">" + "\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
		
		mstr_tab += "\t";
	}
	
	static  public  void	saveEndXMLTag(String str_tag, FileWriter cl_f) {
		int		li_temp = mstr_tab.lastIndexOf("\t");
		
		if(li_temp != -1) {
			mstr_tab = mstr_tab.substring(0, li_temp);
		}
		
		String  lstr_out = "</" + str_tag + ">" + "\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
		
		
	}
	
	static  public  void saveXMLComment(String str_comment, FileWriter cl_f) {
		String  lstr_out = "<!-- " + str_comment + " -->" + "\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
		
		
	}
	
	static  public  void saveXMLHeader(FileWriter cl_f) {
		String	lstr_out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
		
		mstr_tab = "";
	}
	
	/**
	 * attrs must be in key value pairs
	 * @param str_tag
	 * @param cl_f
	 * @param stra_attrs
	 */
	static	public	void saveEmptyTagWithAttributes(String str_tag, FileWriter cl_f, String... stra_attrs) {
		String		lstr_out = "<" + str_tag;
		
		for(int li_index = 0; li_index < stra_attrs.length;) {
			lstr_out += " " + stra_attrs[li_index++] + "=";
			lstr_out += "\"" + stra_attrs[li_index++] + "\"";
		}
		
		lstr_out += " />\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		} catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
	}
	
	static	public	void saveStartTagWithAttributes(String str_tag, FileWriter cl_f, String... stra_attrs) {
		String		lstr_out = "<" + str_tag;
		
		for(int li_index = 0; li_index < stra_attrs.length;) {
			lstr_out += " " + stra_attrs[li_index++] + "=";
			lstr_out += "\"" + stra_attrs[li_index++] + "\"";
		}
		
		lstr_out += ">\n";
		
		try {
			cl_f.write(mstr_tab + lstr_out);
		}
		catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
		
		mstr_tab += "\t";
	}
	
	static	public	void saveTagWithAttributesAndData(String str_tag, String str_data, FileWriter cl_f, String... stra_attrs) {
		String		lstr_out = "<" + str_tag;
		
		for(int li_index = 0; li_index < stra_attrs.length;) {
			lstr_out += " " + stra_attrs[li_index++] + "=";
			lstr_out += "\"" + stra_attrs[li_index++] + "\"";
		}
		
		lstr_out += ">" + str_data + "</" + str_tag + ">";
		
		try {
			cl_f.write(mstr_tab + lstr_out + "\n");
		} catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
	}
	
	static	public	void saveTagWithData(String str_tag, String str_data, FileWriter cl_f) {
		String		lstr_out = "<" + str_tag;
		
		lstr_out += ">" + str_data + "</" + str_tag + ">";
		
		try {
			cl_f.write(mstr_tab + lstr_out + "\n");
		} catch(Exception cl_e) {
			cl_e.printStackTrace();
		}
	}
	
	
}
